﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aulac.Client.Data.XmlQuery;
using Aulac.Global.Xml;
using System.Xml;

namespace Aulac.Client.Controls.Forms.Bss
{
    public partial class FormBssCmdBaseDataItem : FormBssCmdData
    {
        ClientExecXmlQueryDataCatalog mQueryXml = null;
        public FormBssCmdBaseDataItem(IFormListData pFrmList):base(pFrmList)
        {
            InitializeComponent();

        }
        public FormBssCmdBaseDataItem()
        {
            InitializeComponent();
        }
        #region Overrider Methods
        /// <summary>
        /// khoi tao
        /// </summary>
        public override void OnFormLoad()
        {
            mListCtrlData = GetListAulacControlsInForm();
            this.StrVerb = Aulac.Verb.AulacVerb.Catalog.QueryCatalog;
            this.StrNoun = Aulac.Verb.AulacNounTypes.AulacSrvDataQuery.AulacCatalog;
            btnNav.AulacNavCtrl.ShowControl(true, btnNav.AulacNavCtrl.SaveButton, btnNav.AulacNavCtrl.CancelButton, btnNav.AulacNavCtrl.ExitButton);
            base.OnFormLoad();
            mQueryXml = new ClientExecXmlQueryDataCatalog();
        }
       
        protected override bool OnShowDetailVoucher()
        {
            mQueryXml.XmlQueryData.SetQueryType(QueryTypes.QuerySelect);
            mQueryXml.XmlQueryData.ResetQuery(mQueryXml.VchLinesElm);
            OnCreateParamFilterSelectData();
            SetQueryInfor();
            mQueryXml.XmlQueryData.SetRequestInfo(this.StrVerb, StrNoun);
            XmlDataDocument xmlDoc = mQueryXml.ExecureQueryData();
            XmlElement eLine = XmlProcess.GetFirstItemLine(xmlDoc);

            if (XmlProcess.IsEmpty(eLine))
                return false;
            if(Aulac.Global.Common.ValueHelper.isEmpty(mListCtrlData))
                mListCtrlData = GetListAulacControlsInForm();
            foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in mListCtrlData)
                ctr.SetValueToControl(eLine);

            return true;
        }

        private void SetQueryInfor()
        {
            string sTable = "", sQueryString = "", sFilterString = "";
            OnSetQueryDataInfo(ref sTable, ref sQueryString, ref sFilterString);
            if (Aulac.Global.Common.ValueHelper.isEmpty(sTable))
                sTable = mFrmListData.TableName;
            mQueryXml.XmlQueryData.SetTableName(mQueryXml.VchLinesElm, sTable);
            mQueryXml.XmlQueryData.SetQueryString(mQueryXml.VchLinesElm, sQueryString);
            mQueryXml.XmlQueryData.SetFilterString(mQueryXml.VchLinesElm, sFilterString);
        }
       
        /// <summary>
        /// Add
        /// </summary>
        protected override void OnAddNewVoucher()
        {
            mQueryXml.XmlQueryData.SetQueryType(QueryTypes.AddDataTable);
        }
       
        /// <summary>
        /// Edit
        /// </summary>
        protected override void OnEditVoucher()
        {
            mQueryXml.XmlQueryData.SetQueryType(QueryTypes.UpdateDataTable);
            
        }
       
        /// <summary>
        /// Save
        /// </summary>
        /// <param name="xmlData">Du lieu can Save</param>
        /// <returns>True neu thanh cong</returns>
        protected override bool OnSaveVoucherData(ref XmlDataDocument xmlData)
        {
            OnCreateFieldSaveDataToServer();
            OnCreateFieldPrimaryKey();
            if (FormMode == Aulac.Global.Enums.FormModes.EditMode)
            {
                OnCreateFieldCheckValidDataUpdate();
            }
            OnCreateFieldGetDataResult();
            OnCreateDataSaveToServer(mQueryXml.VchLinesElm);

            SetQueryInfor();

            mQueryXml.XmlQueryData.SetRequestInfo(this.StrVerb, StrNoun);
            xmlData = mQueryXml.ExecureQueryData();
            return true;
        }
        
        /// <summary>
        /// override Xoa item
        /// </summary>
        /// <returns></returns>
        protected override bool OnDeleteDataVoucher()
        {
            mQueryXml.XmlQueryData.SetQueryType(QueryTypes.DelDataTable);
            OnCreateParamFilterDelData();
            SetQueryInfor();
            return mQueryXml.ExecureNoneQuery();
        }

        /// <summary>
        /// Overrde khoi tao param truy van select du lieu
        /// </summary>
        protected virtual void OnCreateParamFilterSelectData()
        {

        }
        
        /// <summary>
        /// Override method nay de khoi tao cac Field can Save du lieu len server
        /// </summary>
        protected virtual void OnCreateFieldSaveDataToServer()
        {

        }
       
        /// <summary>
        /// Override de khoi tao cac Field Primary key khi Add hoac update data
        /// </summary>
        protected virtual void OnCreateFieldPrimaryKey()
        {
        }
       
        /// <summary>
        /// Ovrride de khoi tao param del du lieu o server
        /// </summary>
        protected virtual void OnCreateParamFilterDelData()
        {
            
        }
       
        /// <summary>
        /// Ovrride khoi tao du lieu Save len Server
        /// </summary>
        /// <param name="eVchVLines"></param>
        protected virtual void OnCreateDataSaveToServer(XmlElement eVchVLines)
        {
            throw new NotImplementedException();
        }
        
        /// <summary>
        /// override de khoi tao Field check valid khi update du lieu len server
        /// </summary>
        protected virtual void OnCreateFieldCheckValidDataUpdate()
        {

        }
        /// <summary>
        /// Oerride de khoi tao param filter khi Update du lieu len server
        /// </summary>
        protected virtual void OnCreateParamFilterUpdateData()
        {
        }

        /// <summary>
        /// override khoi tao Field lay cac can lay gia tri tra ve
        /// </summary>
        protected virtual void OnCreateFieldGetDataResult()
        {
        }

        /// <summary>
        /// Override Set cac gia tri truy van neu can thiet
        /// </summary>
        /// <param name="pTableName">Ten Table truy van</param>
        /// <param name="pSqlQueryString">Chuoi truy van</param>
        /// <param name="pFilterString"Chuoi Filter></param>
        protected virtual void OnSetQueryDataInfo(ref string pTableName, ref string pSqlQueryString, ref string pFilterString)
        {
            pTableName = mFrmListData.TableName;
        }
        #endregion
        
        #region Properties
 
        private string mStrVerb = Aulac.Verb.AulacVerb.Catalog.GetListDataItems;
        public string StrVerb
        {
            set { mStrVerb = value; }
            get { return mStrVerb; }
        }
        
        private string mStrNoun = Aulac.Verb.AulacVerb.Catalog.Noun.AulacCatalog;
        public string StrNoun
        {
            set { mStrNoun = value; }
            get { return mStrNoun; }
        }

        #endregion
        #region Public Method
        /// <summary>
        /// Add gia tri Param de Filter du lieu
        /// </summary>
        /// <param name="pColName">Field truy van</param>
        /// <param name="pParamName">ten param</param>
        /// <param name="pDataType">Kieu du lieu</param>
        /// <param name="pArrParamValue">danh sach gia tri tham so</param>
        public void AddValueParamFilter(string pColName, string pParamName, DbType pDataType, params object[] pArrParamValue)
        {
            XmlElement eLines= mQueryXml.VchLinesElm;
            mQueryXml.XmlQueryData.AddValueParamFilterItem(eLines, pColName, pParamName, pDataType, pArrParamValue);
        }

        /// <summary>
        /// Add value param filter du lieu
        /// </summary>
        /// <param name="pColName">Ten Field</param>
        /// <param name="pParamName">Ten param</param>
        /// <param name="pDataType">Kieu du lieu</param>
        /// <param name="pRefType">Phuong thuc lay du lieu</param>
        public void AddValueParamFilter(string pColName, string pParamName, DbType pDataType, RefValueTypes pRefType)
        {
            mQueryXml.XmlQueryData.AddValueParamFilterItem(mQueryXml.VchLinesElm, pColName, pParamName, pDataType, pRefType);
        }

        /// <summary>
        /// Add sach sach Field CheckValid
        /// </summary>
        /// <param name="pFieldName">Danh sach Field can check</param>
        public void AddFieldCheckValidDataRang(string[] pListFieldName)
        {
            mQueryXml.XmlQueryData.AddFieldCheckValidItemRange(mQueryXml.VchLinesElm, pListFieldName);
        }
        /// <summary>
        /// Add Field check Valid
        /// </summary>
        /// <param name="pFieldName">Ten Field can check</param>
        /// <param name="pDataType">Kieu du lieu</param>
        /// <param name="pCheckType">Phuong thuc check</param>
        /// <param name="pRefValueType">Phuong thuc lay du lieu</param>
        public void AddFieldCheckValidDataUpdate(string pFieldName, DbType pDataType,CheckValidTypes pCheckType, RefValueTypes pRefValueType)
        {
            mQueryXml.XmlQueryData.AddFieldCheckValidItem(mQueryXml.VchLinesElm, pFieldName, pDataType,pCheckType, pRefValueType);
        }

        /// <summary>
        /// Add Field Key
        /// </summary>
        /// <param name="pFieldName">Ten Field</param>
        /// <param name="pDataType">Kieu du lieu</param>
        /// <param name="pKeyType">Kieu Key</param>
        public void AddPrimaryKeyField(string pFieldName, DbType pDataType, KeyTypes pKeyType)
        {
            mQueryXml.XmlQueryData.AddFieldPrk(mQueryXml.VchLinesElm, pFieldName, pDataType, pKeyType);
        }

        /// <summary>
        /// Add danh sach Field can lay ket qua tra ve
        /// </summary>
        /// <param name="pListFields"></param>
        public void AddFieldGetDataResultRange(params string[] pListFields)
        {
            mQueryXml.XmlQueryData.AddFieldNameResultItemRange(mQueryXml.VchLinesElm, pListFields);
        }

        /// <summary>
        /// Set danh sach Fiel truy van du lieu
        /// </summary>
        /// <param name="pListField">Danh sach Field</param>
        public void SetListFieldDataQuery(string[] pListField)
        {
            mQueryXml.XmlQueryData.AddFieldInfoNameRange(mQueryXml.VchLinesElm, pListField);
        }
       
        #endregion

        private void CreateXmlQuery()
        {
            if (mQueryXml == null)
                mQueryXml = new ClientExecXmlQueryDataCatalog();

            mQueryXml.XmlQueryData.SetRequestInfo(StrVerb, StrNoun);
           
            XmlElement eLines = mQueryXml.VchLinesElm;
            mQueryXml.XmlQueryData.RemoveAllParamFilterItem(mQueryXml.VchLinesElm);
            mQueryXml.XmlQueryData.RemoveCheckValidElm(mQueryXml.VchLinesElm);
            
        }
    }
}
